home *** CD-ROM | disk | FTP | other *** search
- PROGRAM PRSave;
-
- {$U-}
- USES PasInOut,Memtypes,QuickDraw,OSIntf,ToolIntf,PackIntf,MacPrint;
-
- CONST
- gPRResType = 'POPT'; (* POPT = Print OPTions. This type can be anything *)
- (* but to avoid confusion with Printing Manager *)
- (* resources, the following types should NOT be *)
- (* used: PREC, PDEF, & POST... *)
- gPRResID = 128; (* This can also be any value. Since there should *)
- (* only be one print record per document, the ID is *)
- (* a constant. *)
- gPRResName = 'Print Record'; (* Resource name. *)
- VAR
- gPrintRecordHdl: THPrint;
- gTargetResFile: INTEGER;
-
- PROCEDURE Debugger; INLINE $A9FF;
-
-
- (* ReportError *)
- (* *)
- (* This procedure is responsible for reporting an error to the user. This is done *)
- (* by first converting the error code passed in theError into a message that can be *)
- (* displayed for the user. See Technical Note #161, "When to call PrOpen and *)
- (* PrClose". *)
- PROCEDURE ReportError(theError: OSErr);
- BEGIN
- (* Real programs handle errors by displayed comprehensible error messages. *)
- (* This is NOT a real program... *)
- SysBeep(10);
- END;
-
-
- (* InitializePrintRecord *)
- (* *)
- (* This procedure is responsible for initializing a newly created print record. *)
- (* It begins by calling PrintDefault to fill in default values, and then presents *)
- (* the standard 'Page Setup' dialog allowing the user to specify page setup options.*)
- (* The modified print record is then returned. *)
- PROCEDURE InitializePrintRecord(thePrintRecord: THPrint);
- VAR
- ignored: BOOLEAN;
- BEGIN
- PrOpen;
- IF PrError = noErr THEN BEGIN
- PrintDefault(thePrintRecord);
- ignored := PrStlDialog(thePrintRecord);
- END;
- PrClose;
- END;
-
-
- (* SavePrintRecord *)
- (* *)
- (* This procedure is responsible for saving a print record into a resource file. *)
- (* On entry, the print record should be initialized, and the resource file should *)
- (* be open with permission to write. *)
- PROCEDURE SavePrintRecord(thePrintRecord: THPrint; theResFile: INTEGER);
- VAR
- currentResFile: INTEGER;
- existingResHdl: Handle;
- newResHdl: Handle;
- theError: OSErr;
- BEGIN
- (* First save the currently selected resource file (before calling UseResFile). *)
- currentResFile := CurResFile;
-
- (* Now select the target resource file. *)
- UseResFile(theResFile);
- theError := ResError;
- IF theError = noErr THEN BEGIN
- existingResHdl := GetResource(gPRResType, gPRResID);
- IF existingResHdl <> NIL THEN BEGIN
- (* There is already a print record resource in this file, so we need to *)
- (* delete it before adding the new one. *)
- RmveResource(existingResHdl);
- theError := ResError;
- IF theError = noErr THEN BEGIN
- (* If the resource was successfully removed, dispose of its memory *)
- (* and update the resource file. *)
- DisposHandle(existingResHdl);
- UpdateResFile(theResFile);
- END;
- END;
-
- IF theError = noErr THEN BEGIN
- (* Okay, now we have successfully opened the file, and deleted any *)
- (* previously saved print record resources. Finally we can add the new *)
- (* one... *)
- (* Since the Resource Manager is going to keep the handle we pass it, *)
- (* we need to make a copy before calling AddResource. We'll let the *)
- (* system do it for us by calling HandToHand. *)
- newResHdl := Handle(thePrintRecord);
- theError := HandToHand(newResHdl);
- IF theError = noErr THEN BEGIN
- AddResource(newResHdl, gPRResType, gPRResID, gPRResName);
- theError := ResError;
- IF theError = noErr THEN UpdateResFile(theResFile);
- theError := ResError;
- END;
- END;
- END;
- IF theError <> noErr THEN
- ReportError(theError);
-
- (* Be polite and restore the original resource file to the top of the chain. *)
- UseResFile(currentResFile);
- END;
-
-
- (* GetPrintRecord *)
- (* *)
- (* This function is responsible for loading a resource containing a valid print *)
- (* record. On entry theResFile should be open with permission to read. *)
- FUNCTION GetPrintRecord(theResFile: INTEGER): THPrint;
- VAR
- currentResFile: INTEGER;
- theResource: Handle;
- theError: OSErr;
- BEGIN
- currentResFile := CurResFile;
- UseResFile(theResFile);
- theError := ResError;
- IF theError = noErr THEN BEGIN
- theResource := GetResource(gPRResType, gPRResID);
- theError := ResError;
- END;
- IF theError <> noErr THEN
- ReportError(theError);
- UseResFile(currentResFile);
- GetPrintRecord := THPrint(theResource);
- END;
-
-
- (* TestPrintRecord *)
- (* *)
- (* This procedure is used to test a print record. It will print a line of text *)
- (* using the options specified in thePrintRecord passed. On exit, a line of text *)
- (* will have been printed. *)
- PROCEDURE TestPrintRecord(thePrintRecord: THPrint);
- VAR
- currentPort: GrafPtr;
- thePMPort: TPPrPort;
- theError: OSErr;
- BEGIN
- GetPort(currentPort);
- PrOpen;
- IF PrError = noErr THEN BEGIN
- IF PrJobDialog(thePrintRecord) THEN BEGIN
- thePMPort := PrOpenDoc(thePrintRecord, NIL, NIL);
- IF PrError = noErr THEN BEGIN
- PrOpenPage(thePMPort, NIL);
- IF PrError = noErr THEN BEGIN
- SetPort(@thePMPort^.gPort);
-
- MoveTo(100, 100);
- DrawString('This is a test...');
- END;
- PrClosePage(thePMPort);
- END;
- PrCloseDoc(thePMPort);
- END;
- END;
- theError := PrError; (* Any errors? *)
- PrClose; (* Close the Printing Manager before attempting *)
- (* to report the error. *)
- IF theError <> noErr THEN (* If there was an error during printing... *)
- ReportError(theError); (* ...report the error to the user. *)
- SetPort(currentPort);
- END;
-
-
- BEGIN
- InitGraf(@thePort); {initialize QuickDraw}
- InitFonts; {initialize Font Manager}
- FlushEvents(everyEvent, 0); {call OS Event Mgr to discard any previous events}
- InitWindows; {initialize Window Manager}
- InitMenus; {initialize Menu Manager}
- TEInit; {initialize TextEdit}
- InitDialogs(NIL); {initialize Dialog Manager}
- InitCursor; {call QuickDraw to make cursor (pointer) an arrow}
-
- (* Get the ID of our resource file. Since we were just opened, the CurResFile *)
- (* will be ours. In a real application, the resource file ID would be the ID *)
- (* of your application's document file. *)
- gTargetResFile := CurResFile;
-
- (* Create a valid print record *)
- gPrintRecordHdl := THPrint(NewHandle(SIZEOF(TPrint)));
- IF gPrintRecordHdl <> NIL THEN BEGIN
- (* Okay, we got a print record, now initialize it. *)
- InitializePrintRecord(gPrintRecordHdl);
-
- (* Now save the print record into the resource file. *)
- SavePrintRecord(gPrintRecordHdl, gTargetResFile);
-
- (* Now that it's saved, kill it off. We'll restore it by *)
- (* calling GetPrintRecord. *)
- DisposHandle(Handle(gPrintRecordHdl));
- gPrintRecordHdl := NIL;
-
- (* Now get the print record from the file. Since the *)
- (* record will be loaded as a resource handle anyway, let *)
- (* GetPrintRecord allocate the handle. *)
- gPrintRecordHdl := GetPrintRecord(gTargetResFile);
- IF gPrintRecordHdl <> NIL THEN BEGIN
- (* Now use the print record to see if the information we *)
- (* saved was preserved... *)
- TestPrintRecord(gPrintRecordHdl);
- END ELSE
- ReportError(MemError);
- END ELSE
- ReportError(MemError);
-
- (* Kill the print record (if it was created) and go home... *)
- IF gPrintRecordHdl <> NIL THEN
- DisposHandle(Handle(gPrintRecordHdl));
- END.
-